## Do speeches at the UN General Assembly affect international aid allocation?
## Quantitative Text Analysis


## This R code generated the "worldscore index" using the text documents of the United Nations 
## speeches

## The text documents of the United Nations speeches can be dowloaded 
## from: https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/0TJX8Y


# clear memory
rm(list=ls())
# set directory
setwd("C:/Users/your_location")

library(classInt)
library(maps)
library(rworldmap)
library(RColorBrewer)
library(countrycode)
library(readtext)
library(quanteda)
library(quanteda.textmodels)
library(ggplot2)
library(ggthemes)
library(tm) 
library(Matrix)
library(plyr)
library(caret)
library(openxlsx)
## quanteda version '2.0.1'
## quanteda.textmodels version '0.9.1'

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 1975-2018/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_corpus_rus <- corpus_subset(ungd_corpus, Country == "RUS")
ungd_corpus_usa <- corpus_subset(ungd_corpus, Country == "USA")

ungd_dfm_rus <- dfm(ungd_corpus_rus,
                    stem = TRUE,
                    remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must", "s", "also"),
                    remove_punct = TRUE,
                    remove_numbers = TRUE)

ungd_dfm_usa <- dfm(ungd_corpus_usa,
                    stem = TRUE,
                    remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must", "s", "also"),
                    remove_punct = TRUE,
                    remove_numbers = TRUE)

ungd_dfm_rus <- dfm_trim(ungd_dfm_rus, min_termfreq = 10, min_docfreq = 5)
ungd_dfm_usa <- dfm_trim(ungd_dfm_usa, min_termfreq = 10, min_docfreq = 5)

topfeatures(ungd_dfm_rus)
topfeatures(ungd_dfm_usa)
topfeatures(ungd_dfm_rus,20)
topfeatures(ungd_dfm_usa,20)
topfeatures(ungd_dfm_usa, n = 100, decreasing = FALSE)

print(ungd_dfm_usa)

summarise((summary(ungd_corpus, n = 7,533, verbose = FALSE)),
                    mean(Types),mean(Tokens),
                   mean(Sentences),min(Sentences),max(Sentences))

############################
## Wordclouds descriptive ##
############################

## 1989

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 44 - 1989/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_corpus_rus <- corpus_subset(ungd_corpus, Country == "RUS")
ungd_corpus_usa <- corpus_subset(ungd_corpus, Country == "USA")

ungd_dfm_rus <- dfm(ungd_corpus_rus,
                    stem = TRUE,
                    remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must", "s", "also"),
                    remove_punct = TRUE,
                    remove_numbers = TRUE)

ungd_dfm_usa <- dfm(ungd_corpus_usa,
                    stem = TRUE,
                    remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must", "s", "also"),
                    remove_punct = TRUE,
                    remove_numbers = TRUE)

topfeatures(ungd_dfm_rus,20)
topfeatures(ungd_dfm_usa,20)

textplot_wordcloud(
  ungd_dfm_rus,
  min_size = 0.5,
  max_size = 6,
  min_count = 3,
  max_words = 130,
  color = "darkred",
  font = NULL,
  adjust = 0,
  rotation = 0.1,
  random_order = FALSE,
  random_color = FALSE,
  ordered_color = FALSE,
  labelcolor = "gray20",
  labelsize = 1.5,
  labeloffset = 0,
  fixed_aspect = TRUE,
  comparison = FALSE
)

textplot_wordcloud(
  ungd_dfm_usa,
  min_size = 0.5,
  max_size = 6,
  min_count = 3,
  max_words = 500,
  color = "darkblue",
  font = NULL,
  adjust = 0,
  rotation = 0.1,
  random_order = FALSE,
  random_color = FALSE,
  ordered_color = FALSE,
  labelcolor = "gray20",
  labelsize = 1.5,
  labeloffset = 0,
  fixed_aspect = TRUE,
  comparison = FALSE
)

## 2002

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 57 - 2002/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_corpus_rus <- corpus_subset(ungd_corpus, Country == "RUS")
ungd_corpus_usa <- corpus_subset(ungd_corpus, Country == "USA")

ungd_dfm_rus <- dfm(ungd_corpus_rus,
                    stem = TRUE,
                    remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must", "s", "also"),
                    remove_punct = TRUE,
                    remove_numbers = TRUE)

ungd_dfm_usa <- dfm(ungd_corpus_usa,
                    stem = TRUE,
                    remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must", "s", "also"),
                    remove_punct = TRUE,
                    remove_numbers = TRUE)

topfeatures(ungd_dfm_rus,20)
topfeatures(ungd_dfm_usa,20)

textplot_wordcloud(
  ungd_dfm_rus,
  min_size = 0.5,
  max_size = 6,
  min_count = 3,
  max_words = 500,
  color = "darkred",
  font = NULL,
  adjust = 0,
  rotation = 0.1,
  random_order = FALSE,
  random_color = FALSE,
  ordered_color = FALSE,
  labelcolor = "gray20",
  labelsize = 1.5,
  labeloffset = 0,
  fixed_aspect = TRUE,
  comparison = FALSE
)

textplot_wordcloud(
  ungd_dfm_usa,
  min_size = 0.5,
  max_size = 6,
  min_count = 3,
  max_words = 500,
  color = "darkblue",
  font = NULL,
  adjust = 0,
  rotation = 0.1,
  random_order = FALSE,
  random_color = FALSE,
  ordered_color = FALSE,
  labelcolor = "gray20",
  labelsize = 1.5,
  labeloffset = 0,
  fixed_aspect = TRUE,
  comparison = FALSE
)


##########################################
### MAKING THE WORDSCORE INDEX BY YEAR ###
##########################################

##################
##USA VS RUSSIA###
##################

##########
## 1971 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 26 - 1971/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1971",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1971.xlsx", append = FALSE)

##########
## 1972 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 27 - 1972/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1972",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1972.xlsx", append = FALSE)

##########
## 1973 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 28 - 1973/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1973",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1973.xlsx", append = FALSE)

##########
## 1974 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 29 - 1974/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1974",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1974.xlsx", append = FALSE)

##########
## 1975 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 30 - 1975/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1975",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1975.xlsx", append = FALSE)

##########
## 1976 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 31 - 1976/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1976",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1976.xlsx", append = FALSE)


##########
## 1977 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 32 - 1977/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1977",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1977.xlsx", append = FALSE)

##########
## 1978 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 33 - 1978/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1978",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1978.xlsx", append = FALSE)

##########
## 1979 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 34 - 1979/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1979",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1979.xlsx", append = FALSE)

##########
## 1980 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 35 - 1980/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1980",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1980.xlsx", append = FALSE)

##########
## 1981 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 36 - 1981/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1981",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1981.xlsx", append = FALSE)

##########
## 1982 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 37 - 1982/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1982",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1982.xlsx", append = FALSE)

##########
## 1983 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 38 - 1983/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1983",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1983.xlsx", append = FALSE)

##########
## 1984 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 39 - 1984/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1984",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1984.xlsx", append = FALSE)

##########
## 1985 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 40 - 1985/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1985",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1985.xlsx", append = FALSE)

##########
## 1986 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 41 - 1986/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1986",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1986.xlsx", append = FALSE)

##########
## 1987 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 42 - 1987/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1987",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1987.xlsx", append = FALSE)

##########
## 1988 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 43 - 1988/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1988",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1988.xlsx", append = FALSE)

##########
## 1989 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 44 - 1989/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1989",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1989.xlsx", append = FALSE)

##########
## 1990 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 45 - 1990/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1990",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1990.xlsx", append = FALSE)

##########
## 1991 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 46 - 1991/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1991",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1991.xlsx", append = FALSE)

##########
## 1992 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 47 - 1992/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1992",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1992.xlsx", append = FALSE)

##########
## 1993 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 48 - 1993/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1993",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1993.xlsx", append = FALSE)

##########
## 1994 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 49 - 1994/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "CHINA vs USA: Wordscores 1994",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1994.xlsx", append = FALSE)

##########
## 1995 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 50 - 1995/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1995",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1995.xlsx", append = FALSE)

##########
## 1996 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 51 - 1996/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1996",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1996.xlsx", append = FALSE)

##########
## 1997 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 52 - 1997/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1997",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1997.xlsx", append = FALSE)

##########
## 1998 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 53 - 1998/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1998",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1998.xlsx", append = FALSE)

##########
## 1999 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 54 - 1999/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1999",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1999.xlsx", append = FALSE)


##########
## 2000 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 55 - 2000/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2000",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2000.xlsx", append = FALSE)


##########
## 2001 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 56 - 2001/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2001",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2001.xlsx", append = FALSE)


##########
## 2002 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 57 - 2002/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2002",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2002.xlsx", append = FALSE)


##########
## 2003 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 58 - 2003/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2003",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2003.xlsx", append = FALSE)

##########
## 2004 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 59 - 2004/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2004",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2004.xlsx", append = FALSE)

##########
## 2005 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 60 - 2005/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2005",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2005.xlsx", append = FALSE)

##########
## 2006 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 61 - 2006/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2006",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2006.xlsx", append = FALSE)


##########
## 2007 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 62 - 2007/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2007",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2007.xlsx", append = FALSE)

##########
## 2008 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 63 - 2008/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2008",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2008.xlsx", append = FALSE)

##########
## 2009 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 64 - 2009/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2009",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2009.xlsx", append = FALSE)

##########
## 2010 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 65 - 2010/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2010",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2010.xlsx", append = FALSE)

##########
## 2011 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 66 - 2011/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2011",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2011.xlsx", append = FALSE)

##########
## 2012 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 67 - 2012/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2012",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2012.xlsx", append = FALSE)

##########
## 2013 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 68 - 2013/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "CHINA vs USA: Wordscores 2013",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2013.xlsx", append = FALSE)

##########
## 2014 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 69 - 2014/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2014",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2014.xlsx", append = FALSE)

##########
## 2015 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 70 - 2015/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2015",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2015.xlsx", append = FALSE)

##########
## 2016 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 71 - 2016/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2016",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2016.xlsx", append = FALSE)

##########
## 2017 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 72 - 2017/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2017",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2017.xlsx", append = FALSE)

##########
## 2018 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 73 - 2018/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 10, min_docfreq = 5)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2018",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2018.xlsx", append = FALSE)

######################################
######################################
##### PLACEBO TRIMMING WORDSCORE #####
######################################
######################################

##################
##USA VS RUSSIA###
##################

##########
## 1971 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 26 - 1971/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1971",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1971P1.xlsx", append = FALSE)

##########
## 1972 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 27 - 1972/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1972",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1972P1.xlsx", append = FALSE)

##########
## 1973 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 28 - 1973/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1973",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1973P1.xlsx", append = FALSE)

##########
## 1974 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 29 - 1974/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1974",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1974P1.xlsx", append = FALSE)

##########
## 1975 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 30 - 1975/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1975",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1975P1.xlsx", append = FALSE)

##########
## 1976 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 31 - 1976/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1976",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1976P1.xlsx", append = FALSE)


##########
## 1977 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 32 - 1977/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1977",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1977P1.xlsx", append = FALSE)

##########
## 1978 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 33 - 1978/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1978",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1978P1.xlsx", append = FALSE)

##########
## 1979 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 34 - 1979/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1979",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1979P1.xlsx", append = FALSE)

##########
## 1980 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 35 - 1980/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1980",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1980P1.xlsx", append = FALSE)

##########
## 1981 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 36 - 1981/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1981",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1981P1.xlsx", append = FALSE)

##########
## 1982 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 37 - 1982/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1982",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1982P1.xlsx", append = FALSE)

##########
## 1983 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 38 - 1983/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1983",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1983P1.xlsx", append = FALSE)

##########
## 1984 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 39 - 1984/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1984",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1984P1.xlsx", append = FALSE)

##########
## 1985 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 40 - 1985/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1985",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1985P1.xlsx", append = FALSE)

##########
## 1986 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 41 - 1986/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1986",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1986P1.xlsx", append = FALSE)

##########
## 1987 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 42 - 1987/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1987",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1987P1.xlsx", append = FALSE)

##########
## 1988 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 43 - 1988/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1988",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1988P1.xlsx", append = FALSE)

##########
## 1989 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 44 - 1989/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1989",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1989P1.xlsx", append = FALSE)

##########
## 1990 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 45 - 1990/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1990",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1990P1.xlsx", append = FALSE)

##########
## 1991 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 46 - 1991/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1991",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1991P1.xlsx", append = FALSE)

##########
## 1992 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 47 - 1992/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1992",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1992P1.xlsx", append = FALSE)

##########
## 1993 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 48 - 1993/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1993",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1993P1.xlsx", append = FALSE)

##########
## 1994 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 49 - 1994/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "CHINA vs USA: Wordscores 1994",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1994P1.xlsx", append = FALSE)

##########
## 1995 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 50 - 1995/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1995",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1995P1.xlsx", append = FALSE)

##########
## 1996 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 51 - 1996/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1996",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1996P1.xlsx", append = FALSE)

##########
## 1997 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 52 - 1997/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1997",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1997P1.xlsx", append = FALSE)

##########
## 1998 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 53 - 1998/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1998",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1998P1.xlsx", append = FALSE)

##########
## 1999 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 54 - 1999/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1999",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1999P1.xlsx", append = FALSE)


##########
## 2000 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 55 - 2000/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2000",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2000P1.xlsx", append = FALSE)


##########
## 2001 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 56 - 2001/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2001",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2001P1.xlsx", append = FALSE)


##########
## 2002 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 57 - 2002/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2002",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2002P1.xlsx", append = FALSE)


##########
## 2003 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 58 - 2003/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2003",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2003P1.xlsx", append = FALSE)

##########
## 2004 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 59 - 2004/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2004",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2004P1.xlsx", append = FALSE)

##########
## 2005 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 60 - 2005/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2005",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2005P1.xlsx", append = FALSE)

##########
## 2006 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 61 - 2006/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2006",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2006P1.xlsx", append = FALSE)


##########
## 2007 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 62 - 2007/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2007",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2007P1.xlsx", append = FALSE)

##########
## 2008 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 63 - 2008/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2008",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2008P1.xlsx", append = FALSE)

##########
## 2009 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 64 - 2009/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2009",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2009P1.xlsx", append = FALSE)

##########
## 2010 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 65 - 2010/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2010",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2010P1.xlsx", append = FALSE)

##########
## 2011 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 66 - 2011/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2011",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2011P1.xlsx", append = FALSE)

##########
## 2012 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 67 - 2012/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2012",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2012P1.xlsx", append = FALSE)

##########
## 2013 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 68 - 2013/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "CHINA vs USA: Wordscores 2013",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2013P1.xlsx", append = FALSE)

##########
## 2014 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 69 - 2014/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2014",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2014P1.xlsx", append = FALSE)

##########
## 2015 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 70 - 2015/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2015",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2015P1.xlsx", append = FALSE)

##########
## 2016 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 71 - 2016/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2016",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2016P1.xlsx", append = FALSE)

##########
## 2017 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 72 - 2017/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2017",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2017P1.xlsx", append = FALSE)

##########
## 2018 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 73 - 2018/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 8, min_docfreq = 3)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2018",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2018P1.xlsx", append = FALSE)

##################
##USA VS RUSSIA###
##################

##########
## 1971 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 26 - 1971/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1971",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1971P2.xlsx", append = FALSE)

##########
## 1972 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 27 - 1972/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1972",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1972P2.xlsx", append = FALSE)

##########
## 1973 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 28 - 1973/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1973",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1973P2.xlsx", append = FALSE)

##########
## 1974 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 29 - 1974/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1974",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1974P2.xlsx", append = FALSE)

##########
## 1975 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 30 - 1975/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1975",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1975P2.xlsx", append = FALSE)

##########
## 1976 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 31 - 1976/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1976",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1976P2.xlsx", append = FALSE)


##########
## 1977 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 32 - 1977/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1977",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1977P2.xlsx", append = FALSE)

##########
## 1978 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 33 - 1978/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1978",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1978P2.xlsx", append = FALSE)

##########
## 1979 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 34 - 1979/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1979",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1979P2.xlsx", append = FALSE)

##########
## 1980 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 35 - 1980/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1980",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1980P2.xlsx", append = FALSE)

##########
## 1981 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 36 - 1981/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1981",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1981P2.xlsx", append = FALSE)

##########
## 1982 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 37 - 1982/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1982",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1982P2.xlsx", append = FALSE)

##########
## 1983 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 38 - 1983/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1983",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1983P2.xlsx", append = FALSE)

##########
## 1984 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 39 - 1984/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1984",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1984P2.xlsx", append = FALSE)

##########
## 1985 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 40 - 1985/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1985",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1985P2.xlsx", append = FALSE)

##########
## 1986 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 41 - 1986/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1986",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1986P2.xlsx", append = FALSE)

##########
## 1987 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 42 - 1987/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1987",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1987P2.xlsx", append = FALSE)

##########
## 1988 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 43 - 1988/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1988",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1988P2.xlsx", append = FALSE)

##########
## 1989 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 44 - 1989/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1989",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1989P2.xlsx", append = FALSE)

##########
## 1990 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 45 - 1990/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1990",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1990P2.xlsx", append = FALSE)

##########
## 1991 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 46 - 1991/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1991",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1991P2.xlsx", append = FALSE)

##########
## 1992 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 47 - 1992/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1992",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1992P2.xlsx", append = FALSE)

##########
## 1993 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 48 - 1993/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1993",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1993P2.xlsx", append = FALSE)

##########
## 1994 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 49 - 1994/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "CHINA vs USA: Wordscores 1994",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1994P2.xlsx", append = FALSE)

##########
## 1995 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 50 - 1995/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1995",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1995P2.xlsx", append = FALSE)

##########
## 1996 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 51 - 1996/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1996",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1996P2.xlsx", append = FALSE)

##########
## 1997 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 52 - 1997/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1997",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1997P2.xlsx", append = FALSE)

##########
## 1998 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 53 - 1998/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1998",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1998P2.xlsx", append = FALSE)

##########
## 1999 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 54 - 1999/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1999",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1999P2.xlsx", append = FALSE)


##########
## 2000 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 55 - 2000/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2000",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2000P2.xlsx", append = FALSE)


##########
## 2001 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 56 - 2001/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2001",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2001P2.xlsx", append = FALSE)


##########
## 2002 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 57 - 2002/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2002",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2002P2.xlsx", append = FALSE)


##########
## 2003 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 58 - 2003/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2003",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2003P2.xlsx", append = FALSE)

##########
## 2004 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 59 - 2004/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2004",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2004P2.xlsx", append = FALSE)

##########
## 2005 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 60 - 2005/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2005",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2005P2.xlsx", append = FALSE)

##########
## 2006 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 61 - 2006/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2006",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2006P2.xlsx", append = FALSE)


##########
## 2007 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 62 - 2007/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2007",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2007P2.xlsx", append = FALSE)

##########
## 2008 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 63 - 2008/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2008",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2008P2.xlsx", append = FALSE)

##########
## 2009 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 64 - 2009/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2009",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2009P2.xlsx", append = FALSE)

##########
## 2010 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 65 - 2010/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2010",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2010P2.xlsx", append = FALSE)

##########
## 2011 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 66 - 2011/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2011",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2011P2.xlsx", append = FALSE)

##########
## 2012 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 67 - 2012/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2012",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2012P2.xlsx", append = FALSE)

##########
## 2013 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 68 - 2013/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "CHINA vs USA: Wordscores 2013",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2013P2.xlsx", append = FALSE)

##########
## 2014 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 69 - 2014/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2014",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2014P2.xlsx", append = FALSE)

##########
## 2015 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 70 - 2015/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2015",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2015P2.xlsx", append = FALSE)

##########
## 2016 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 71 - 2016/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2016",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2016P2.xlsx", append = FALSE)

##########
## 2017 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 72 - 2017/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2017",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2017P2.xlsx", append = FALSE)

##########
## 2018 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 73 - 2018/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 4, min_docfreq = 2)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2018",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2018P2.xlsx", append = FALSE)

##################
##USA VS RUSSIA###
##################

##########
## 1971 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 26 - 1971/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1971",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1971P3.xlsx", append = FALSE)

##########
## 1972 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 27 - 1972/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1972",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1972P3.xlsx", append = FALSE)

##########
## 1973 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 28 - 1973/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1973",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1973P3.xlsx", append = FALSE)

##########
## 1974 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 29 - 1974/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1974",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1974P3.xlsx", append = FALSE)

##########
## 1975 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 30 - 1975/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1975",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1975P3.xlsx", append = FALSE)

##########
## 1976 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 31 - 1976/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1976",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1976P3.xlsx", append = FALSE)


##########
## 1977 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 32 - 1977/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1977",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1977P3.xlsx", append = FALSE)

##########
## 1978 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 33 - 1978/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1978",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1978P3.xlsx", append = FALSE)

##########
## 1979 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 34 - 1979/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1979",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1979P3.xlsx", append = FALSE)

##########
## 1980 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 35 - 1980/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1980",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1980P3.xlsx", append = FALSE)

##########
## 1981 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 36 - 1981/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1981",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1981P3.xlsx", append = FALSE)

##########
## 1982 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 37 - 1982/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1982",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1982P3.xlsx", append = FALSE)

##########
## 1983 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 38 - 1983/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1983",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1983P3.xlsx", append = FALSE)

##########
## 1984 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 39 - 1984/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1984",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1984P3.xlsx", append = FALSE)

##########
## 1985 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 40 - 1985/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1985",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1985P3.xlsx", append = FALSE)

##########
## 1986 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 41 - 1986/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1986",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1986P3.xlsx", append = FALSE)

##########
## 1987 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 42 - 1987/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1987",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1987P3.xlsx", append = FALSE)

##########
## 1988 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 43 - 1988/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1988",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1988P3.xlsx", append = FALSE)

##########
## 1989 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 44 - 1989/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1989",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1989P3.xlsx", append = FALSE)

##########
## 1990 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 45 - 1990/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1990",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1990P3.xlsx", append = FALSE)

##########
## 1991 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 46 - 1991/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1991",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1991P3.xlsx", append = FALSE)

##########
## 1992 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 47 - 1992/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1992",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1992P3.xlsx", append = FALSE)

##########
## 1993 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 48 - 1993/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1993",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1993P3.xlsx", append = FALSE)

##########
## 1994 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 49 - 1994/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "CHINA vs USA: Wordscores 1994",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1994P3.xlsx", append = FALSE)

##########
## 1995 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 50 - 1995/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1995",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1995P3.xlsx", append = FALSE)

##########
## 1996 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 51 - 1996/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1996",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1996P3.xlsx", append = FALSE)

##########
## 1997 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 52 - 1997/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1997",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1997P3.xlsx", append = FALSE)

##########
## 1998 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 53 - 1998/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1998",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1998P3.xlsx", append = FALSE)

##########
## 1999 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 54 - 1999/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1999",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1999P3.xlsx", append = FALSE)


##########
## 2000 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 55 - 2000/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2000",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2000P3.xlsx", append = FALSE)


##########
## 2001 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 56 - 2001/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2001",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2001P3.xlsx", append = FALSE)


##########
## 2002 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 57 - 2002/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2002",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2002P3.xlsx", append = FALSE)


##########
## 2003 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 58 - 2003/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2003",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2003P3.xlsx", append = FALSE)

##########
## 2004 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 59 - 2004/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2004",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2004P3.xlsx", append = FALSE)

##########
## 2005 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 60 - 2005/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2005",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2005P3.xlsx", append = FALSE)

##########
## 2006 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 61 - 2006/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2006",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2006P3.xlsx", append = FALSE)


##########
## 2007 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 62 - 2007/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2007",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2007P3.xlsx", append = FALSE)

##########
## 2008 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 63 - 2008/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2008",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2008P3.xlsx", append = FALSE)

##########
## 2009 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 64 - 2009/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2009",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2009P3.xlsx", append = FALSE)

##########
## 2010 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 65 - 2010/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2010",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2010P3.xlsx", append = FALSE)

##########
## 2011 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 66 - 2011/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2011",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2011P3.xlsx", append = FALSE)

##########
## 2012 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 67 - 2012/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2012",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2012P3.xlsx", append = FALSE)

##########
## 2013 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 68 - 2013/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "CHINA vs USA: Wordscores 2013",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2013P3.xlsx", append = FALSE)

##########
## 2014 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 69 - 2014/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2014",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2014P3.xlsx", append = FALSE)

##########
## 2015 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 70 - 2015/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2015",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2015P3.xlsx", append = FALSE)

##########
## 2016 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 71 - 2016/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2016",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2016P3.xlsx", append = FALSE)

##########
## 2017 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 72 - 2017/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2017",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2017P3.xlsx", append = FALSE)

##########
## 2018 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 73 - 2018/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 12, min_docfreq = 8)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2018",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2018P3.xlsx", append = FALSE)

##################
##USA VS RUSSIA###
##################

##########
## 1971 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 26 - 1971/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1971",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1971P4.xlsx", append = FALSE)

##########
## 1972 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 27 - 1972/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1972",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1972P4.xlsx", append = FALSE)

##########
## 1973 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 28 - 1973/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1973",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1973P4.xlsx", append = FALSE)

##########
## 1974 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 29 - 1974/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1974",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1974P4.xlsx", append = FALSE)

##########
## 1975 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 30 - 1975/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1975",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1975P4.xlsx", append = FALSE)

##########
## 1976 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 31 - 1976/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1976",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1976P4.xlsx", append = FALSE)


##########
## 1977 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 32 - 1977/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1977",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1977P4.xlsx", append = FALSE)

##########
## 1978 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 33 - 1978/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1978",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1978P4.xlsx", append = FALSE)

##########
## 1979 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 34 - 1979/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1979",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1979P4.xlsx", append = FALSE)

##########
## 1980 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 35 - 1980/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1980",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1980P4.xlsx", append = FALSE)

##########
## 1981 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 36 - 1981/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1981",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1981P4.xlsx", append = FALSE)

##########
## 1982 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 37 - 1982/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1982",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1982P4.xlsx", append = FALSE)

##########
## 1983 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 38 - 1983/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1983",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1983P4.xlsx", append = FALSE)

##########
## 1984 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 39 - 1984/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1984",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1984P4.xlsx", append = FALSE)

##########
## 1985 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 40 - 1985/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1985",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1985P4.xlsx", append = FALSE)

##########
## 1986 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 41 - 1986/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1986",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1986P4.xlsx", append = FALSE)

##########
## 1987 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 42 - 1987/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1987",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1987P4.xlsx", append = FALSE)

##########
## 1988 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 43 - 1988/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1988",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1988P4.xlsx", append = FALSE)

##########
## 1989 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 44 - 1989/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1989",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1989P4.xlsx", append = FALSE)

##########
## 1990 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 45 - 1990/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1990",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1990P4.xlsx", append = FALSE)

##########
## 1991 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 46 - 1991/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1991",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1991P4.xlsx", append = FALSE)

##########
## 1992 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 47 - 1992/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1992",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1992P4.xlsx", append = FALSE)

##########
## 1993 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 48 - 1993/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1993",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1993P4.xlsx", append = FALSE)

##########
## 1994 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 49 - 1994/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "CHINA vs USA: Wordscores 1994",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1994P4.xlsx", append = FALSE)

##########
## 1995 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 50 - 1995/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1995",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1995P4.xlsx", append = FALSE)

##########
## 1996 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 51 - 1996/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1996",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1996P4.xlsx", append = FALSE)

##########
## 1997 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 52 - 1997/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1997",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1997P4.xlsx", append = FALSE)

##########
## 1998 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 53 - 1998/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1998",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1998P4.xlsx", append = FALSE)

##########
## 1999 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 54 - 1999/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 1999",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_1999P4.xlsx", append = FALSE)


##########
## 2000 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 55 - 2000/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2000",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2000P4.xlsx", append = FALSE)


##########
## 2001 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 56 - 2001/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2001",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2001P4.xlsx", append = FALSE)


##########
## 2002 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 57 - 2002/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2002",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2002P4.xlsx", append = FALSE)


##########
## 2003 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 58 - 2003/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2003",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2003P4.xlsx", append = FALSE)

##########
## 2004 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 59 - 2004/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2004",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2004P4.xlsx", append = FALSE)

##########
## 2005 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 60 - 2005/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2005",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2005P4.xlsx", append = FALSE)

##########
## 2006 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 61 - 2006/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2006",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2006P4.xlsx", append = FALSE)


##########
## 2007 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 62 - 2007/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2007",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2007P4.xlsx", append = FALSE)

##########
## 2008 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 63 - 2008/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2008",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2008P4.xlsx", append = FALSE)

##########
## 2009 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 64 - 2009/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2009",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2009P4.xlsx", append = FALSE)

##########
## 2010 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 65 - 2010/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2010",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2010P4.xlsx", append = FALSE)

##########
## 2011 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 66 - 2011/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2011",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2011P4.xlsx", append = FALSE)

##########
## 2012 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 67 - 2012/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2012",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2012P4.xlsx", append = FALSE)

##########
## 2013 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 68 - 2013/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "CHINA vs USA: Wordscores 2013",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2013P4.xlsx", append = FALSE)

##########
## 2014 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 69 - 2014/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2014",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2014P4.xlsx", append = FALSE)

##########
## 2015 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 70 - 2015/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2015",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2015P4.xlsx", append = FALSE)

##########
## 2016 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 71 - 2016/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2016",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2016P4.xlsx", append = FALSE)

##########
## 2017 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 72 - 2017/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2017",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2017P4.xlsx", append = FALSE)

##########
## 2018 ##
##########

# clear memory
rm(list=ls())
setwd("C:/Users/your_location")

## IMPORT THE TEXT INTO R 
ungd_debates <- readtext("Session 73 - 2018/*.txt",
                         ignore_missing_files = TRUE,
                         docvarsfrom="filenames",
                         dvsep = "_",
                         docvarnames=c("Country"," Session","Year"),
                         verbosity = 0)  

ungd_corpus <- corpus(ungd_debates)
ungd_dfm <- dfm(ungd_corpus,
                stem = TRUE,
                remove = c(stopwords("english"), "will", "can", "???", "�", "T", "must","s", "also"),
                remove_punct = TRUE,
                remove_numbers = TRUE)

ungd_dfm <- dfm_trim(ungd_dfm, min_termfreq = 15, min_docfreq = 10)

RUS_index <- which(ungd_debates$Country == "RUS")
USA_index <- which(ungd_debates$Country == "USA")

refscores <- rep(NA, nrow(ungd_dfm))
refscores[RUS_index] <- 1
refscores[USA_index] <- -1
refscores

wordscores_model <- textmodel_wordscores(ungd_dfm,
                                         refscores,
                                         scale = "linear",
                                         smooth = 1)

wordscores <- predict(wordscores_model , rescaling = "mv")
ungd_data <- as.data.frame(docvars(ungd_debates))
ungd_data$wordscore <- wordscores

head(ungd_data)

class_intervals <- classIntervals(ungd_data$wordscore,
                                  rtimes = 10,
                                  style = 'bclust')

spatial_data <- joinCountryData2Map(ungd_data,
                                    joinCode = "ISO3",
                                    nameJoinColumn = "Country")

wordscore_map <- mapCountryData(spatial_data,
                                nameColumnToPlot = "wordscore",
                                catMethod = class_intervals$brks,
                                mapTitle = "RUS vs USA: Wordscores 2018",
                                colourPalette = brewer.pal(9, "Reds"),
                                missingCountryCol = "grey",
                                addLegend = FALSE)

do.call(addMapLegend, c(wordscore_map,
                        legendLabels = "limits",
                        labelFontSize = 0.7,
                        legendShrink = 0.7,
                        legendMar = 5,
                        legendWidth = 0.5))

ungd_data$wordscore_int <- cut(ungd_data$wordscore,
                               include.lowest = TRUE,
                               breaks = class_intervals$brks)


write.xlsx(ungd_data, file = "USAungd_data_2018P4.xlsx", append = FALSE)




